package com.app.util.json;

import java.io.IOException;
import java.io.StringWriter;
import java.util.Collection;
import java.util.Map;

import com.app.util.collection.MapUtil;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

/**
 * json工具类
 * 
 * @author yao
 */
public class JSONUtil {

	private final ObjectMapper objectMapper;
	private final JsonFactory jsonFactory;

	public JSONUtil() {
		this(null);
	}

	public JSONUtil(StdSerializer<?>[] stdSerializers) {
		objectMapper = new ObjectMapper();
		if (null != stdSerializers) {
			SimpleModule testModule = new SimpleModule();
			for (StdSerializer<?> stdSerializer : stdSerializers) {
				testModule.addSerializer(stdSerializer);
			}
			objectMapper.registerModule(testModule);
		}
		jsonFactory = objectMapper.getFactory();
	}

	/**
	 * 将对象转换成json字符串
	 * 
	 * @param o
	 *            对象支持Collection及array
	 * @return jsonString
	 * @throws JsonMappingException
	 */
	public String parserString(Object o) {
		if (null == o) {
			return null;
		}
		final StringWriter stringWriter = new StringWriter(1024);
		try {
			JsonGenerator jg = jsonFactory.createGenerator(stringWriter);
			jg.writeObject(o);
		} catch (IOException e) {
			return null;
		}
		return stringWriter.toString();
	}

	/**
	 * 将对象转换成json字符串
	 * 
	 * @param o
	 *            对象支持Collection及array
	 * @return jsonString
	 * @throws JsonMappingException
	 */
	public <T> T parserObject(String jsonString, Class<T> objectClass) {
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			return jp.readValueAs(objectClass);
		} catch (IOException e) {
			return null;
		}
	}

	/**
	 * 将json字符串转换成指定类的对象集合，支持(? extends Collection)
	 * 
	 * @param jsonString
	 * @param objectClass
	 * @param collectionType
	 * @return
	 */
	public <T> Collection<T> parseCollection(String jsonString, Class<T> objectClass, Class<? extends Collection> collectionType) {
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			Collection<?> jsonObjList = ((Collection<?>) jp.readValueAs(collectionType));
			final Collection<T> objList = (Collection<T>) collectionType.newInstance();
			for (Object obj : jsonObjList) {
				if (obj instanceof Map) {
					T o = MapUtil.parseObject((Map) obj, objectClass);
					objList.add(o);
				} else {
					objList.add((T) obj);
				}
			}
			return objList;
		} catch (IOException e) {
			return null;
		} catch (InstantiationException e) {
			throw new RuntimeException(e);
		} catch (IllegalAccessException e) {
			throw new RuntimeException(e);
		}
	} 

	/**
	 * 将json对象字符串转换成Map，属性名作为key，属性值作为value
	 * 
	 * @param jsonString
	 * @param clazz
	 * @return
	 */
	public Map parseMap(String jsonString, Class<? extends Map> clazz) {
		try {
			JsonParser jp = jsonFactory.createParser(jsonString);
			return jp.readValueAs(clazz);
		} catch (IOException e) {
			return null;
		}
	}
}
